<?php
class SNSGroupMember extends Eloquent {
	protected $table = 'sns_group_member';
	public function user()
	{
		return $this->belongsTo('User','user_id');
	}

	/**
	 * 增加圈子用户
	 * 
     *@param $group_id 圈子id
     *@param $user_id  用户id
	 */
	public static function addGroupMember($group_id, $user_id) {
		$sns_group = SNSGroup::find($group_id);
		$user = User::find($user_id);

		$sns_group_member = new SNSGroupMember;
		$sns_group_member->group_id = $group_id;
		$sns_group_member->user_id = $user_id;
		$sns_group_member->is_admin = 0;
		$sns_group_member->is_creator = 0;
		$sns_group_member->is_passed = 1;

		if ($user->user_group_id == 1) {
			$sns_group->increment('fans_cnt');
		} elseif ($user->user_group_id == 2) {
			$sns_group->increment('lawyers_cnt');
		}

		DB::transaction(function() use($sns_group_member, $sns_group, $user_id){
			$sns_group->save();
			$sns_group_member->save();

			// 同步环信数据
			$result = new Easemob();
			$group = $result->addGroupsUser($sns_group->easemob_id, $user_id);
		});
	}
}